package com.cml;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 姓名：陈茂林
 * 时间：2024/4/2
 * 描述：
 */
public class Test {
    public static void main(String[] args) {
        String msg = (String) IntStream.rangeClosed(0, 1000)
                .boxed()
                .map(Object::toString).collect(Collectors.joining(","));
        ByteBuf byteBuf =Unpooled.buffer(4+msg.length());
        byteBuf.writeInt(msg.length());
        byteBuf.writeBytes(msg.getBytes());

        byteBuf.readerIndex(0);
        byte[] hand  = new byte[4];
        byteBuf.readBytes(hand);
        System.out.println(new String(hand));

        byte[] body1  = new byte[byteBuf.readableBytes()-1000];
        byteBuf.readBytes(body1);
        System.out.println(new String(body1));
        byte[] body2  = new byte[1000];
        byteBuf.readBytes(body2);

        ByteBuf byteBuf2 = Unpooled.buffer(byteBuf.readableBytes());
        byteBuf2.writeBytes(body1);
        byteBuf2.writeBytes(body2);

        byteBuf.readerIndex(0);
        byte[] body3  = new byte[body1.length+body2.length];
        byteBuf2.readBytes(body3);
        System.out.println(new String(body3));
    }
}
